package com.example.owner.lettery1.ui;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.example.owner.lettery1.R;
import com.example.owner.lettery1.base.BaseActivity;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class Banner1Activity extends BaseActivity {

    WebView mWebview1;
    WebSettings mWebSettings;
    TextView loading;
    @Override
    public int getLayoutId() {
        return R.layout.activity_banner1;
    }

    @Override
    protected void init() {
        /*mWebview1 = findViewById(R.id.web_view1);
        loading = findViewById(R.id.text_Loading);*/
        mWebSettings = mWebview1.getSettings();

        mWebview1.loadUrl("http://m.cpb11.com/index.php/phone/youhuiItem/id/1/type/8");

        //设置不用系统浏览器打开,直接显示在当前Webview
        mWebview1.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
        });


        mWebview1.setWebChromeClient(new WebChromeClient(){
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                if (newProgress < 100) {
                    String progress = newProgress + "%";
                    loading.setText(progress);
                } else if (newProgress == 100) {
                    String progress = newProgress + "%";
                    loading.setText(progress);
                }
            }
        });

    }



    //点击返回上一页面而不是退出浏览器
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && mWebview1.canGoBack()) {
            mWebview1.goBack();
            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    //销毁Webview
    @Override
    protected void onDestroy() {
        if (mWebview1 != null) {
            mWebview1.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
            mWebview1.clearHistory();

            ((ViewGroup) mWebview1.getParent()).removeView(mWebview1);
            mWebview1.destroy();
            mWebview1 = null;
        }
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.bt_back)
    public void onViewClicked() {
        finish();
    }
}
